<template>
    <div class="main">
        <van-list v-model="loading"
                  :finished="finished"
                  :error.sync="error"
                  finished-text="我是有底线的!"
                  error-text="请求失败，点击重新加载"
                  @load="onLoad">
            <tk-goods v-for="item in list" :key="item.id" :data="item"></tk-goods>
        </van-list>
    </div>
</template>

<script>
    import {List} from 'vant';
    import Goods from '../components/Goods.vue';

    export default {
        data() {
            return {
                url: this.$store.api.list + '/4093',
                page: 1,
                rows: 20,
                list: [],
                finished: false,
                loading: false,
                error: false
            }
        },
        components: {
            [List.name]: List,
            [Goods.name]: Goods,

        },
        methods: {
            onLoad() {
                this.request(this.url, {
                    page: this.page++,
                    rows: this.rows
                });
            },
            async request(url, params) {
                this.loading = true;
                try {
                    const result = await this.$http.getStore(url, params);
                    this.list.push(...result['data']);
                } catch (e) {
                    this.error = true;
                }
                this.loading = false;
            }
        },
        watch: {
            page() {
                if (this.page > 20) this.finished = true;
            }
        }
    }
</script>
<style scoped>
    .van-list {
        margin: 10px;
    }
</style>
